# encoding=utf-8
from utils import *
from enum_type import *


class Order:
    def __init__(self):
        """
        单个委托的数据结构
        """
        self.invest_unit_id = None
        self.instance_id = None
        self.order_id = next_uuid()
        self.strategy_req_id = next_time_id()
        self.symbol = None
        self.market = None
        self.market_no = None
        self.price = None
        self.quantity = None
        self.side = None
        self.price_type = None
        self.direction = None
        self.future_directions = None

    def create(self,
               invest_unit_id: int,
               instance_id: int,
               symbol: str,
               price: float,
               quantity: int,
               side: str,
               price_type: PriceType,
               futures_direction=None,
               ):
        self.invest_unit_id = invest_unit_id
        self.instance_id = instance_id
        self.order_id = next_uuid()
        self.symbol = symbol
        # self.strategy_req_id = int(int(self.symbol[3:6]) * 1e6 + next_time_id())
        self.strategy_req_id = 0
        self.market = Market.SH if '.XSHG' in self.symbol else Market.SZ
        self.market_no = '1' if self.market == Market.SH else '2'
        if isinstance(price, int):
            price = float(price)
        self.price = price if isinstance(price, float) else 0
        self.quantity = quantity
        self.side = side
        self.price_type = price_type
        self.direction = 'OPEN' if self.side in(Side.Buy, Side.Bond_Buy) else 'CLOSE'
        # if futures_direction:
        #     self.direction = futures_direction
        # print('input ', futures_direction, 'self.direction', self.direction)
        return self

    def get_order(self):
        """
        获取订单
        :return:
        """
        return self.build_order()

    def build_order(self) -> (list, list):
        fields = ['investunit_id', 'instance_id', 'iqe_symbol', 'entrust_direction', 'futures_direction',
                  'close_direction',
                  'futu_invest_type', 'price_type', 'entrust_price', 'entrust_amount', 'ext_remark', 'module_id',
                  'extsystem_id',
                  'limit_ratio_future', 'limit_ratio_option', 'limit_entrust_ratio', 'strategy_req_id',
                  'strategy_flag_id',
                  'capitalunit_id', 'market_no', 'stock_code']
        data = [
            [self.invest_unit_id, self.instance_id, self.symbol, self.side, self.direction, '', '1',
             self.price_type, self.price, self.quantity, self.order_id, 9999, 0, 0, 0, 0, self.strategy_req_id, 0, 0,
             self.market_no, self.symbol[:self.symbol.index('.')]]]
        return fields, data
